home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b14 / Startup / Startup.c next >
Text File  |  1996-04-11  |  10KB  |  436 lines

  1. /*****
  2.  *
  3.  *    Startup.c
  4.  *
  5.  *    If you want to exclude CGI or ListSTAR support, remove the appropriate lines
  6.  *    from the 'StartupApplication' function.
  7.  *
  8.  *    This is a support file for "Grant's CGI Framework".
  9.  *    Please see the license agreement that accompanies the distribution package
  10.  *    for licensing details.
  11.  *
  12.  *    Copyright ©1995,1996 by Grant Neufeld
  13.  *    grant@acm.com
  14.  *    http://arpp.carleton.ca/grant/
  15.  *
  16.  *****/
  17.  
  18. #include "MyConfiguration.h"
  19.  
  20. #include <Threads.h>
  21. #include <Traps.h>
  22.  
  23. #include "compiler_stuff.h"
  24. #include "constants.h"
  25. #include "globals.h"
  26.  
  27. #include "AboutBox.h"
  28. #include "AEFunc.h"
  29. #include "AEHandlers.h"
  30. #include "CGI.h"
  31. #include "ErrorUtil.h"
  32. #include "EventUtil.h"
  33. #include "ListSTAR.h"
  34. #include "LogUtil.h"
  35. #include "MemoryUtil.h"
  36. #include "MenuFunc.h"
  37. #include "ProcessUtil.h"
  38. #include "SplashScreen.h"
  39. #include "StringUtil.h"
  40. #include "Version.h"
  41.  
  42. #include "Startup.h"
  43.  
  44.  
  45. /***  LOCAL CONSTANTS ***/
  46.  
  47. #define    TrapMask        0x0800
  48. #define kGestaltMask    1L
  49.  
  50. /* window info */
  51. #define    kScreenBorder    4    /* pixels */
  52. #define    kIconSize        32
  53.  
  54.  
  55. /***  LOCAL PROTOTYPES ***/
  56.  
  57. static    void        startupErrors            ( void );
  58. static    void        startupGestalt            ( void );
  59. static    void        startupEvent            ( void );
  60. static    void        startupAE                ( void );
  61. #if kCompileWithForeground
  62. static    void        startupMenus             ( void );
  63. static    void        startupWindows            ( void );
  64. static    void        startupQuickDraw        ( void );
  65. #if kCompileWithDragNDrop
  66. static    void        startupDrag                ( void );
  67. #endif
  68.  
  69. #else
  70.     /* make the interface function prototypes null */
  71.     #define startupMenus()    
  72.     #define startupWindows()    
  73.     #define startupQuickDraw()    
  74. #endif    /* kCompileWithForeground */
  75.     
  76. static    Boolean        startupTrapAvailable    ( unsigned long );
  77. static    TrapType    startupTrapType            ( unsigned long );
  78.  
  79.  
  80. /***  FUNCTIONS  ***/
  81.  
  82. void
  83. StartupApplication ( void )
  84. {
  85.     OSErr    theErr;
  86.     
  87.     /* moved to ProcessStartup() */
  88. //    gSleepTicks        = kSleepTicks;    
  89. //    gFrontProcess    = ProcessCurrentIsFront ();
  90. //    #if kCompileWithProcessFileSpec
  91. //    ProcessGetMyFSSpec ( &gProcessFSSpec );
  92. //    #endif
  93.     
  94.     ProcessStartup        ();
  95.     startupErrors        ();
  96.     
  97.     VersionGetShort        ( 1, gVersionStr );    /* must be before splash screen */
  98.     
  99.     LogStartup            ();    /* set up the log file */
  100.     
  101.     SplashScreenCreate    ();    /* put up splash screen */
  102.  
  103.     startupGestalt        ();    /* must be before all Manager inits except toolbox and memory */
  104.     startupEvent        ();
  105.     startupAE            ();
  106.     startupMenus        ();
  107.     ThreadStartup        ();
  108.     startupWindows        ();    /* must be after startupMenus */
  109.     startupQuickDraw    ();
  110.     #if kCompileWithDragNDrop
  111.     startupDrag            ();
  112.     #endif
  113.     AboutBoxInit        ();
  114.     
  115.     #if kCompileWithQuitOnLongIdle
  116.     gDoIdleQuit        = true;    /* quit after given period of idle time */
  117.     gTimeLastAction = nil;    /* no action has been performed yet */
  118.     gIdleTimeToQuit    = kIdleTimeToQuit;
  119.     gDoIdleQuitOnOpenApp    = kCompileWithIdleQuitOnOpenApp;
  120.     #endif
  121.     
  122.     theErr = InitCGIUtil ();    /* CGI Specific */
  123.     if ( theErr != noErr )
  124.     {
  125.         ErrorStartup ( kerrStartupCGI );
  126.     }
  127.     
  128.     theErr = InitListSTARUtil ();    /* ListSTAR Specific */
  129.     if ( theErr != noErr )
  130.     {
  131.         ErrorStartup ( kerrStartupListSTAR );
  132.     }
  133.     
  134.     /* • the result of this call should be checked to see whether
  135.         initialization was successful */
  136.     theErr = CustomStartup ();
  137.     if ( theErr != noErr )
  138.     {
  139.         ErrorStartup ( kerrStartupCustomInit );
  140.     }
  141.     
  142.     SplashScreenDispose ();    /* remove splash screen */
  143. } /* StartupApplication */
  144.  
  145.  
  146. /***  Initialization Functions  ***/
  147. #pragma mark -
  148.  
  149. /* Initialize default string for system errors */
  150. static void
  151. startupErrors ( void )
  152. {
  153.     StringHandle    tempStr;
  154.     
  155.     /* set system error default string */
  156.     
  157.     gSystemErrorStr    = (StringHandle) MemoryNewHandle ( sizeof(Str255), NULL );
  158.     if ( gSystemErrorStr == NULL )
  159.     {
  160.         return;
  161.     }
  162.     
  163.     HLockHi ( (Handle)gSystemErrorStr );
  164.     
  165.     tempStr = GetString ( krErrSystemDefault );
  166.     if ( tempStr == NULL )
  167.     {
  168.         StringPascalCopy ( (char *)ksErrSystemDefault, (char *)(*gSystemErrorStr) );
  169.     }
  170.     else
  171.     {
  172.         HLock ( (Handle)tempStr );
  173.         
  174.         StringPascalCopy ( (char *)(*tempStr), (char *)(*gSystemErrorStr) );
  175.         
  176.         HUnlock            ( (Handle)tempStr );
  177.         ReleaseResource    ( (Handle)tempStr );
  178.     }
  179.     
  180.     HUnlock ( (Handle)gSystemErrorStr );
  181. } /* startupErrors */
  182.  
  183.  
  184. /* Determine if the Gestalt manager is available */
  185. static void
  186. startupGestalt ( void )
  187. {
  188.     Boolean        gestaltAvail;
  189.     
  190.     /* determine whether the Gestalt call is available */
  191.     gestaltAvail = startupTrapAvailable ( _Gestalt );
  192.     if ( !gestaltAvail )
  193.     {
  194.         /* Gestalt require for application to work, inform the user and exit */
  195.         ErrorStartup ( kerrStartupGestalt );
  196.     }
  197. } /* startupGestalt */
  198.  
  199.  
  200. /* Determine if WaitNextEvent trap is available. */
  201. static void
  202. startupEvent ( void )
  203. {
  204.     OSErr        theErr;
  205.     SysEnvRec    theSysEnv;    /* system environment */
  206.     Boolean        WNEAvail;
  207.     
  208.     /* what are we running on here (System 4.1 or greater) */
  209.     theErr = SysEnvirons ( 1, &theSysEnv );
  210.     if ( theErr != noErr )
  211.     {
  212.         /* can't figure out what the system environment is,
  213.             inform the user and exit this application */
  214.         ErrorStartup ( kerrStartupSysEnv );
  215.     }
  216.     else
  217.     {
  218.         /* is WaitNextEvent implemented? - from Macintosh Tech Note #158 */
  219.         if ( theSysEnv.machineType > envMachUnknown )
  220.         {
  221.             WNEAvail = NGetTrapAddress(0x60, ToolTrap) != NGetTrapAddress(0x9F, ToolTrap);
  222.         }
  223.         else
  224.         {
  225.             WNEAvail = false;
  226.         }
  227.         
  228.         if ( !WNEAvail )
  229.         {
  230.             /* WaitNextEvent trap is missing - can't run */
  231.             ErrorStartup ( kerrStartupWNE );
  232.         }
  233.     }
  234. } /* startupEvent */
  235.  
  236.  
  237. /* Determine if Apple Event Manager is present and install handlers */
  238. static void
  239. startupAE ( void )
  240. {
  241.     long    feature;
  242.     OSErr    theErr;
  243.     
  244.     theErr = Gestalt ( gestaltAppleEventsAttr, &feature );
  245.     if ( (theErr != noErr) ||
  246.         !(feature & (kGestaltMask << gestaltAppleEventsPresent)) )
  247.     {
  248.         /* Apple Events not available, inform the user and exit */
  249.         ErrorStartup ( kerrStartupAppleEvent );
  250.     }
  251.  
  252.     gAEIdleUPP = NewAEIdleProc ( AEFuncAEIdleFunc );
  253.     
  254.     theErr = AEInstallHandlers ();
  255.     if ( theErr != noErr )
  256.     {
  257.         ErrorSystem ( theErr );
  258.     }
  259. } /* startupAE */
  260.  
  261.  
  262. /* setup menus */
  263. #if kCompileWithForeground
  264. static void
  265. startupMenus ( void )
  266. {
  267.     Handle    theMenuBar;
  268.     
  269.     theMenuBar = GetNewMBar ( kmMenuBarID );
  270.     if ( theMenuBar == NULL )
  271.     {
  272.         /* can't load menu bar */
  273.         ErrorStartup ( kerrStartupMenu );
  274.     }
  275.     
  276.     SetMenuBar ( theMenuBar );
  277.  
  278.     gmAppleMenu    = GetMHandle ( kmAppleMenuID );
  279.     /* add "Apple Menu Items" to the Apple menu */
  280.     AddResMenu    ( gmAppleMenu, 'DRVR' );
  281.  
  282.     gmFileMenu    = GetMHandle ( kmFileMenuID );
  283.     gmEditMenu    = GetMHandle ( kmEditMenuID );
  284.     
  285.     DrawMenuBar ();
  286. } /* startupMenus */
  287. #endif    /* kCompileWithForeground */
  288.  
  289.  
  290. /* Determine the space available on the main and other monitors */
  291. #if kCompileWithForeground
  292. static void
  293. startupWindows ( void )
  294. {
  295.     short         resW;
  296.     short        resH;
  297.     short        menuBarHeight;
  298.     RgnHandle    grayRgnHdl;
  299.     Rect        grayRgnBounds;
  300.     
  301.     /* get screen dimensions */
  302.     resW = qd.screenBits.bounds.right  - qd.screenBits.bounds.left;
  303.     resH = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  304.     
  305.     menuBarHeight = LMGetMBarHeight ();
  306.     
  307.     /* gScreenRect is the size of the main screen, inset by a margin */
  308.     SetRect ( &gScreenRect, kScreenBorder, menuBarHeight + kScreenBorder, 
  309.         resW - kScreenBorder, resH - kScreenBorder );
  310.         
  311.     /* get entire gray rgn rect (the size of all monitors) */
  312.     grayRgnHdl        = GetGrayRgn ();            /* known as the 'gray region' */
  313.     grayRgnBounds    = (*grayRgnHdl)->rgnBBox;    /* the bounding box for the region */
  314.     
  315.     resW = grayRgnBounds.right  - grayRgnBounds.left;
  316.     resH = grayRgnBounds.bottom - grayRgnBounds.top;
  317.     
  318.     /* this rect encompasses all of the monitor desktop space,
  319.         minus the menu bar and inset by a margin. We'll use this
  320.         rectangle to limit dragging and resizing windows */
  321.     SetRect ( &gGrayRgnRect,
  322.         /* left */        kScreenBorder,
  323.         /* top */        menuBarHeight + kScreenBorder, 
  324.         /* right */        resW - kScreenBorder,
  325.         /* bottom */    resH - kScreenBorder );
  326. } /* startupWindows */
  327. #endif    /* kCompileWithForeground */
  328.  
  329.  
  330. /* QuickDraw - Graphics */
  331. #if kCompileWithForeground
  332. static void
  333. startupQuickDraw ( void )
  334. {
  335.     OSErr    theErr;
  336.     long    feature;
  337.     long    version;
  338.     
  339.     gHasColorQD = false;
  340.     
  341.     theErr = Gestalt ( gestaltQuickdrawFeatures, &feature );
  342.     if ( theErr == noErr )
  343.     {
  344.         theErr = Gestalt ( gestaltQuickdrawVersion, &version );
  345.         if ( (theErr == noErr) &&
  346.             ((feature & (kGestaltMask << gestaltHasColor)) != nil) &&
  347.             (version >= gestalt8BitQD) )
  348.         {
  349.             gHasColorQD = true;
  350.         }
  351.     }
  352. } /* startupQuickDraw */
  353. #endif    /* kCompileWithForeground */
  354.  
  355.  
  356. /* Drag Manager - interapplication drag'n'drop */
  357. #if kCompileWithDragNDrop
  358. static void
  359. startupDrag ( void )
  360. {
  361.     long    feature;
  362.     OSErr    theErr;
  363.     
  364.     theErr = Gestalt ( gestaltDragMgrAttr, &feature );
  365.     if ( (theErr == noErr) &&
  366.         (feature & (kGestaltMask << gestaltDragMgrPresent)) )
  367.     {
  368.         gHasDragNDrop = true;
  369.     }
  370.     else
  371.     {
  372.         gHasDragNDrop = false;
  373.     }
  374. } /* startupAE */
  375. #endif
  376.  
  377.  
  378. #pragma mark -
  379.  
  380. /* Determine if a trap is available */
  381. static Boolean
  382. startupTrapAvailable ( unsigned long trap )
  383. {
  384.     UniversalProcPtr    theInitGrafAddr;
  385.     UniversalProcPtr    theAA6EAddr;
  386.     Boolean                trapAddrMatch;
  387.     unsigned long        numToolBoxTraps;
  388.     TrapType            theTrapType;
  389.     unsigned long        theTrapMasked;
  390.     
  391.     theInitGrafAddr    = NGetTrapAddress ( _InitGraf, ToolTrap );
  392.     theAA6EAddr        = NGetTrapAddress ( 0xAA6E, ToolTrap );
  393.     trapAddrMatch    = theInitGrafAddr == theAA6EAddr;
  394.     
  395.     if ( trapAddrMatch )
  396.     {
  397.         numToolBoxTraps = 0x200;
  398.     }
  399.     else
  400.     {
  401.         numToolBoxTraps = 0x400;
  402.     }
  403.     
  404.     theTrapType = startupTrapType ( trap );
  405.     if ( theTrapType == ToolTrap )
  406.     {
  407.         theTrapMasked = trap & 0x07FF;
  408.         if ( theTrapMasked >= numToolBoxTraps )
  409.         {
  410.             return false;
  411.         }
  412.     }
  413.     
  414.     return NGetTrapAddress ( _Unimplemented, ToolTrap ) !=
  415.             NGetTrapAddress ( trap, theTrapType );
  416. } /* startupTrapAvailable */
  417.  
  418.  
  419. /* determine the type of the trap */
  420. static TrapType
  421. startupTrapType ( unsigned long theTrap )
  422. {
  423.     /* OS traps start with A0, Tool with A8 or AA. */
  424.     if ( (theTrap & 0x0800) == nil )
  425.     {
  426.         return OSTrap;
  427.     }
  428.     else
  429.     {
  430.         return ToolTrap;
  431.     }
  432. } /* startupTrapType */
  433.  
  434.  
  435. /*****  EOF  *****/
  436.